package org.mch.func.img.cut;

import java.awt.event.ActionEvent;
import java.io.File;
import java.io.IOException;
import java.util.Map;

import javax.swing.JFileChooser;

import org.mch.biz.ui.FuncEvent;
import org.mch.biz.ui.FuncPanel;
import org.mch.swingm.core.MFileChooser;

import com.mch.main.common.MEnv;

/**
 * 描述：文件操作；
 * 
 * @author MCH；Date：2008-10-01；<BR/>
 * 
 */
public class ClientEvent extends FuncEvent {
	private static final long serialVersionUID = -8590108060646853821L;

	MFileChooser fileChooser;

	public ClientEvent(FuncPanel panel) {
		super(panel);
	}

	protected void onButtonClick(ActionEvent e) throws Exception {
		if (e.getSource() == getPanel().getBtn_brower()) {
			browse();
		}
		if (e.getSource() == getPanel().getBtn_cutimg()) {
			execute();
		}
	}

	public void execute() throws IOException {
		String filestr = getPanel().getText_folder().getText();
		File folder = new File(filestr);

		final File[] files = folder.listFiles();
		final int count = files.length;
		int size = count / 10;

		for (int i = 0; i < size; i++) {
			final File[] tmpfiles = new File[10];
			System.arraycopy(files, i * 10, tmpfiles, 0, 10);

			Thread thread = new Thread(new Runnable() {
				@Override
				public void run() {
					cutImgFile(tmpfiles);
					System.out.println(Thread.currentThread().getName() + "：执行完成！");
				}
			});
			thread.setName("裁剪图片线程[" + i + "]");
			thread.start();
		}

		int shenyu = count - (size * 10);
		if (shenyu > 0) {
			final File[] tmpfiles = new File[shenyu];
			System.arraycopy(files, size * 10, tmpfiles, 0, shenyu);

			Thread thread = new Thread(new Runnable() {
				@Override
				public void run() {
					cutImgFile(tmpfiles);
					System.out.println(Thread.currentThread().getName() + "：执行完成！");
				}
			});
			thread.setName("裁剪图片线程[余下]");
			thread.start();
		}
	}

	private void cutImgFile(File[] tmpfiles) {
		OperateImage oim = new OperateImage();
		for (int i = 0, len = tmpfiles.length; i < len; i++) {
			File f = tmpfiles[i];
			if (f.isDirectory()) {
				File[] imgfiles = f.listFiles();
				cutImgFile(imgfiles);
			} else if (f.isFile()) {
				try {
					oim.cutImageTest(f, f);
				} catch (IOException e) {
					System.out.println("异常：" + e.getMessage() + f.getPath());
				}
			}
		}

	}

	protected void renameFileMap(Map<File, File> map) {
		if (map == null || map.isEmpty()) {
			return;
		}
		File[] keyfiles = map.keySet().toArray(new File[0]);
		for (int i = 0, len = keyfiles.length; i < len; i++) {
			keyfiles[i].renameTo(map.get(keyfiles[i]));
		}
	}

	public void browse() {
		int tmp = getFileChooser().showOpenDialog(MEnv.MAINFRAME);
		if (tmp == JFileChooser.APPROVE_OPTION) {
			getPanel().getText_folder().setText(getFileChooser().getSelectedFile().getPath());
		}
	}

	public MFileChooser getFileChooser() {
		if (fileChooser == null) {
			fileChooser = new MFileChooser();
			fileChooser.setPreferredSize(700, 400);
			fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);// 设置能选择文件和文件夹
		}
		return fileChooser;
	}

	public ClientPanel getPanel() {
		return (ClientPanel) super.getPanel();
	}
}